package com.dashu.core.controller;

import com.dashu.base.enums.ReplRyCode;
import com.dashu.base.enums.school.excel.ExcelName;
import com.dashu.base.enums.school.excel.ExcelType;
import com.dashu.base.utils.excel.ExcelUtil;
import com.dashu.core.dto.PageDTO;
import com.dashu.core.dto.attendance.*;
import com.dashu.core.result.PageEnty;
import com.dashu.core.result.ResultBean;
import com.dashu.core.service.AtService;
import com.dashu.core.service.AttendanceRuleService;
import com.dashu.core.service.AttendanceTimeService;
import com.dashu.core.vo.attendancek.*;
import com.dashu.core.vo.UpdateEntityVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Author xys
 * @Date 2020/ReplRyCode.SUCCESS.getCode()ReplRyCode.SUCCESS.getCode()/ReplRyCode.SUCCESS.getCode()7 ReplRyCode.SUCCESS.getCode()5:2ReplRyCode.SUCCESS.getCode()
 */

@RequestMapping("dashu/attendancek")
@RestController
@Slf4j
@Api(value = "dashu/attendancek", tags = {"考勤模块"}, description = "后台")
public class AttendanceController {

    @Autowired
    private AttendanceRuleService attendanceRuleService;
    @Autowired
    private AttendanceTimeService attendanceTimeService;
    @Autowired
    private AtService atService;
//    @Autowired
//    RestTemplate restTemplate;

    @PostMapping("/addRule")
    @ApiOperation(value = "考勤规则添加", notes = "考勤规则添加")
    public ResultBean<AttendanceRuleDTO> addRule(@RequestBody @Validated AttendanceRuleVO attendanceRuleVO) {
        AttendanceRuleDTO attendanceRuleDTO = attendanceRuleService.existByName(attendanceRuleVO.getAtBaseId(),attendanceRuleVO.getRuleName());
        if (attendanceRuleDTO != null) {
            return new ResultBean<>(ReplRyCode.EXIST_NAME.getCode(), ReplRyCode.EXIST_NAME.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "考勤规则添加", attendanceRuleService.add(attendanceRuleVO));
    }

    @PostMapping("/updataRule")
    @ApiOperation(value = "考勤规则信息修改", notes = "考勤规则信息修改")
    public ResultBean<AttendanceRuleDTO> updataCls(@RequestBody @Validated UpdateEntityVO<AttendanceRuleVO> updateAttendancekRuleVO) throws Exception {
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "考勤规则信息修改", attendanceRuleService.updataByAttendancekRuleId(updateAttendancekRuleVO));
    }


    @DeleteMapping("/deleteRule")
    @ApiOperation(value = "删除考勤规则信息", notes = "删除考勤规则信息")
    public ResultBean<Boolean> deleteRule(long attendancekRuleId) throws Exception {
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "删除考勤规则信息", attendanceRuleService.deleteByAttendancekRuleId(attendancekRuleId));

    }

    @GetMapping("/findRule")
    @ApiOperation(value = "考勤规则信息查询", notes = "考勤规则信息查询")
    public ResultBean<AttendanceRuleDTO> findRule(Long attendancekRuleId) {
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "考勤规则信息查询", attendanceRuleService.findByAttendancekRuleId(attendancekRuleId));

    }

    @PostMapping("/findRuleList")
    @ApiOperation(value = "考勤规则信息列表查询", notes = "考勤规则信息列表查询")
    public ResultBean<PageDTO<AttendanceRuleListDTO>> findRuleList(@RequestBody @Validated PageEnty<AttendanceRuleListVO> rulePageEntyListVO) {
        PageDTO<AttendanceRuleListDTO> pageDTO = new PageDTO<>();
        long total = attendanceRuleService.ruleListCount(rulePageEntyListVO);
        pageDTO.setTotal(total);
        pageDTO.setDataList(attendanceRuleService.pageAttendancekRuleList(rulePageEntyListVO));
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "考勤规则信息列表查询", pageDTO);

    }


    @PostMapping("/addTime")
    @ApiOperation(value = "时间组添加", notes = "时间组添加")
    public ResultBean<AttendanceTimeDTO> addTime(@RequestBody @Validated AttendanceTimeVO attendanceTimeVO) {
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "时间组添加", attendanceTimeService.add(attendanceTimeVO));
    }

    @PostMapping("/updataTime")
    @ApiOperation(value = "时间组信息修改", notes = "时间组信息修改")
    public ResultBean<AttendanceTimeDTO> updataTime(@RequestBody @Validated UpdateEntityVO<AttendanceTimeVO> attendancekDateVO) throws Exception {
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "时间组信息修改", attendanceTimeService.updataAttendanceData(attendancekDateVO));
    }


    @DeleteMapping("/deleteTime")
    @ApiOperation(value = "删除时间组信息", notes = "删除时间组信息")
    public ResultBean<Boolean> deleteTime(long timeId) throws Exception {
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "删除时间组信息", attendanceTimeService.deleteAttendancekData(timeId));

    }

    @PostMapping("/findTime")
    @ApiOperation(value = "时间组信息查询", notes = "时间组信息查询")
    public ResultBean<AttendanceTimeDTO> findTime(long timeId) {
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "时间组信息查询", attendanceTimeService.findAttendancekGroup(timeId));

    }

    @PostMapping("/findTimeList")
    @ApiOperation(value = "时间组列表查询", notes = "时间组列表查询")
    public ResultBean<PageDTO<AttendanceTimeDTO>> findTimeList(@RequestBody @Validated PageEnty<AttendanceTimeListVO> attendanceTimeListVO) throws Exception {
        PageDTO<AttendanceTimeDTO> pageDTO = new PageDTO<>();
        long total = attendanceTimeService.count(attendanceTimeListVO);
        pageDTO.setTotal(total);
        pageDTO.setDataList(attendanceTimeService.findTimeList(attendanceTimeListVO));
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "时间组列表查询", pageDTO);

    }


    @PostMapping("/techaeAtInfo")
    @ApiOperation(value = "教师考勤统计查询", notes = "教师考勤统计查询")
    public ResultBean<AtTeacherInfoDTO> techaeAtInfo(long companyId, long attendanceNum) throws Exception {
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "教师考勤统计查询", atService.techaeAtInfo(companyId, attendanceNum));

    }

    @PostMapping("/techaeInfo")
    @ApiOperation(value = "教师考勤详情查询", notes = "教师考勤详情查询")
    public ResultBean<TeacherInfoListDTO> techaeInfo(@RequestBody @Validated AtInfoVO atInfoVO) throws Exception {
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "教师考勤详情查询", atService.findTechaeInfo(atInfoVO));

    }

    @PostMapping("/techaeInfoList")
    @ApiOperation(value = "教师考勤列表查询", notes = "教师考勤列表查询")
    public ResultBean<AtTeacherInfoListDTO> techaeInfoList(@RequestBody @Validated AtTeacherInfoListVO atTeacherInfoListVO) throws Exception {
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "教师考勤列表查询", atService.findTechaerInfoList(atTeacherInfoListVO));

    }

    @PostMapping("/teacherWeekMonth")
    @ApiOperation(value = "教师周月考勤列表查询", notes = "教师周月考勤列表查询")
    public ResultBean<AtTeacherInfoListDTO> teacherWeekMonth(@RequestBody @Validated AtTeacharInfoWeekMonthVO atTeacharInfoWeekMonthVO) throws Exception {
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "教师考勤列表查询", atService.teacherWeekMonth(atTeacharInfoWeekMonthVO));

    }


    @GetMapping("/exportTeacherDay")
    @ApiOperation(value = "导出教师当日报表", notes = "导出教师当日报表")
    public void exportTeacherDay(HttpServletResponse response, long companyId, long time) throws Exception {

        atService.exportTeacherDay(response, companyId, time);
    }

    @GetMapping("/exportTeacherMonth")
    @ApiOperation(value = "导出教师当月报表", notes = "导出教师当月报表")
    public void exportTeacherMonth(HttpServletResponse response, long companyId, long time) throws Exception {
        atService.exportTeacherMonth(response, companyId,time);
    }


    @PostMapping("/studentAtInfo")
    @ApiOperation(value = "学生考勤统计查询", notes = "学生考勤统计查询")
    public ResultBean<AtStudentInfoDTO> studentAtInfo(long companyId, long attendanceNum) throws Exception {
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "学生考勤统计查询", atService.studentAtInfo(companyId, attendanceNum));

    }

    @PostMapping("/studentInfo")
    @ApiOperation(value = "学生考勤详情查询", notes = "学生考勤详情查询")
    public ResultBean<StudentInfoListDTO> studentInfo(@RequestBody @Validated AtInfoVO atInfoVO) throws Exception {
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "学生考勤详情查询", atService.findStudentInfo(atInfoVO));

    }

    @PostMapping("/studentInfoList")
    @ApiOperation(value = "学生考勤列表查询", notes = "学生考勤列表查询")
    public ResultBean<AtStudentInfoListDTO> studentInfoList(@RequestBody @Validated AtStudentInfoListVO atStudentInfoListVO) throws Exception {
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "学生考勤列表查询", atService.findStudentInfoList(atStudentInfoListVO));

    }

    @PostMapping("/studentWeekMonth")
    @ApiOperation(value = "学生周月考勤列表查询", notes = "学生周月考勤列表查询")
    public ResultBean<AtStudentInfoListDTO> studentWeekMonth(@RequestBody @Validated AtStudentInfoWeekMonthVO atStudentInfoWeekMonthVO) throws Exception {
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "学生周月考勤列表查询", atService.studentWeekMonth(atStudentInfoWeekMonthVO));
    }


    @GetMapping("/exportStudentDay")
    @ApiOperation(value = "导出学生当日报表", notes = "导出学生当日报表")
    public void exportStudentDay(HttpServletResponse response, long companyId, long time) throws Exception {
        atService.exportStudentDay(response, companyId,time);
    }

    @GetMapping("/exportStudentMonth")
    @ApiOperation(value = "导出学生当月报表", notes = "导出学生当月报表")
    public void exportStudentMonth(HttpServletResponse response, long companyId, long time) throws Exception {
        atService.exportStudentMonth(response, companyId,time);
    }


    @GetMapping("/exportDay")
    @ApiOperation(value = "导出当报表日", notes = "导出当报表日")
    public void exportDay(HttpServletResponse response) {
        List<Map<String, String>> excelMaps = new ArrayList<>();
        Map<String, String> map = new HashMap<>();
        map.put("personalName", "肖勇士");
        map.put("num", "15989477179");
        map.put("personalType", "开跑车上学");
        map.put("cls", "1022");
        map.put("icon", "http://dashu-ai.oss-cn-shenzhen.aliyuncs.com/null189802C04-7204V00212-20201119115701.jpg?Expires=1924765778&OSSAccessKeyId=LTAINMu9jTyyU2FD&Signature=Wibn5hBvQaWMT1rpkc3c0GOrXU4%3D");
        map.put("statusDay", "异常");
        map.put("start", "2020.11.20 07:59");
        map.put("startStatus", "迟到");
        map.put("startPerature", "36.8");
//        map.put("startIcon", "http://dashu-ai.oss-cn-shenzhen.aliyuncs.com/null189802C04-7204V00212-20201119115701.jpg?Expires=1924765778&OSSAccessKeyId=LTAINMu9jTyyU2FD&Signature=Wibn5hBvQaWMT1rpkc3c0GOrXU4%3D");
        map.put("end", "2020.11.20 07:59");
        map.put("endStatus", "迟到");
        map.put("endPerature", "迟到");
        map.put("endIcon", "http://dashu-ai.oss-cn-shenzhen.aliyuncs.com/null189802C04-7204V00212-20201119115701.jpg?Expires=1924765778&OSSAccessKeyId=LTAINMu9jTyyU2FD&Signature=Wibn5hBvQaWMT1rpkc3c0GOrXU4%3D");
        map.put("start1", "2020.11.20 07:59");
        map.put("startStatus1", "迟到");
        map.put("startPerature1", "36.8");
        map.put("startIcon1", "http://dashu-ai.oss-cn-shenzhen.aliyuncs.com/null189802C04-7204V00212-20201119115701.jpg?Expires=1924765778&OSSAccessKeyId=LTAINMu9jTyyU2FD&Signature=Wibn5hBvQaWMT1rpkc3c0GOrXU4%3D");
        map.put("end1", "2020.11.20 07:59");
        map.put("endStatus1", "迟到");
        map.put("endPerature1", "36.8");
        map.put("endIcon1", "http://dashu-ai.oss-cn-shenzhen.aliyuncs.com/null189802C04-7204V00212-20201119115701.jpg?Expires=1924765778&OSSAccessKeyId=LTAINMu9jTyyU2FD&Signature=Wibn5hBvQaWMT1rpkc3c0GOrXU4%3D");
        map.put("start2", "2020.11.20 07:59");
        map.put("startStatus2", "迟到");
        map.put("startPerature2", "36.8");
        map.put("startIcon2", "http://dashu-ai.oss-cn-shenzhen.aliyuncs.com/null189802C04-7204V00212-20201119115701.jpg?Expires=1924765778&OSSAccessKeyId=LTAINMu9jTyyU2FD&Signature=Wibn5hBvQaWMT1rpkc3c0GOrXU4%3D");
        map.put("end2", "2020.11.20 07:59");
        map.put("endStatus2", "迟到");
        map.put("endPerature2", "36.8");
        map.put("endIcon2", "http://dashu-ai.oss-cn-shenzhen.aliyuncs.com/null189802C04-7204V00212-20201119115701.jpg?Expires=1924765778&OSSAccessKeyId=LTAINMu9jTyyU2FD&Signature=Wibn5hBvQaWMT1rpkc3c0GOrXU4%3D");
        excelMaps.add(map);
        excelMaps.add(map);
        excelMaps.add(map);
        ExcelUtil.exportData(response, ExcelName.ATTENDANCEK_DAY, ExcelType.STUDENT, excelMaps);
    }

    @GetMapping("/exportMonth")
    @ApiOperation(value = "导出当月报表", notes = "导出当月报表")
    public void exportMonth(HttpServletResponse response) {
        List<Map<String, String>> excelMaps = new ArrayList<>();
        Map<String, String> map = new HashMap<>();
        map.put("personalName", "肖勇士");
        map.put("num", "15989477179");
        map.put("personalType", "开跑车上学");
        map.put("cls", "1022");
        map.put("icon", "http://dashu-ai.oss-cn-shenzhen.aliyuncs.com/null189802C04-7204V00212-20201119115701.jpg?Expires=1924765778&OSSAccessKeyId=LTAINMu9jTyyU2FD&Signature=Wibn5hBvQaWMT1rpkc3c0GOrXU4%3D");

        excelMaps.add(map);
        excelMaps.add(map);
        excelMaps.add(map);
        ExcelUtil.exportData(response, ExcelName.ATTENDANCEK_MONTH, ExcelType.STUDENT, excelMaps);
    }
}
